# -*- coding: UTF-8 -*- 
from xml.etree import ElementTree as ET
import os, sys

def get_home_dir():
    tmp_dir = sys.argv[0].split('/')
    home_dir = ''
    for i in range(len(tmp_dir) - 2):
	home_dir = home_dir + tmp_dir[i] + '/'
    return home_dir

def deal_info_add(f_xml, subdeal, li_from, li_to, tabsum):
    subdeal_li = subdeal.getiterator(li_from)

    for node in subdeal_li:
	f_xml.write(('\t' * tabsum + '<%s>%s</%s>\n' % (li_to, node.text, li_to)).encode('utf-8'))

def deal_info_source(f_xml, name, full, url, invite):
    f_xml.write('\t' * 2 + '<source>\n')
    f_xml.write(('\t' * 3 + '<name>%s</name>\n' % (name)).encode('utf-8'))
    f_xml.write(('\t' * 3 + '<full>%s</full>\n' % (full)).encode('utf-8'))
    f_xml.write(('\t' * 3 + '<url>%s</url>\n' % (url)).encode('utf-8'))
    f_xml.write(('\t' * 3 + '<invite>%s</invite>\n' % (invite)).encode('utf-8'))
    f_xml.write('\t' * 2 + '</source>\n')

def deal_info_cats(f_xml, deal, source):
    deal_title = deal.getiterator(source)
    title = ''
    for node in deal_title:
	title += node.text

    f_kw = open('%slist/keyword' % (get_home_dir()), 'r')
    while 1 + 1 == 2:
	keyword = f_kw.readline()
	if not len(keyword):
	    break

	keyword = keyword.split(':')
	if title.find(keyword[0].decode('utf-8')) > -1:
	    f_kwl = open('%slist/keywordlist' % (get_home_dir()), 'r')
	    keywordlist = eval(f_kwl.read())

	    f_xml.write(('\t' * 2 + '<cats>%s</cats>\n' % (keywordlist[keyword[0]].decode('utf-8'))).encode('utf-8'))

	    return
    
    f_xml.write('\t' * 2 + '<cats>other</cats>\n')

def deal_info_vendor_1(f_xml,deal):
   f_xml.write('\t' * 2 + '<vendor>\n')

   vendor_name = deal.getiterator('vendor_name')
   if len(vendor_name):
       f_xml.write(('\t' * 3 + '<name>%s</name>\n' % (vendor_name[0].text)).encode('utf-8'))

   vendor_url = deal.getiterator('vendor_website_url')
   if len(vendor_url):
       if str(vendor_url[0].text) == 'None':
	   f_xml.write(('\t' * 3 + '<url><![CDATA[https://encrypted.google.com/search?hl=zh-CN&q=%s]]></url>\n' % (vendor_name[0].text)).encode('utf-8'))
       else:
	   f_xml.write(('\t' * 3 + '<url>%s</url>\n' % (vendor_url[0].text)).encode('utf-8'))

   f_xml.write('\t' * 2 + '</vendor>\n')

def deal_info_vendor_2(f_xml):
   f_xml.write('\t' * 2 + '<vendor>\n')
   f_xml.write('\t' * 3 + '<name></name>\n')
   f_xml.write('\t' * 3 + '<url></url>\n')
   f_xml.write('\t' * 2 + '</vendor>\n')

def deal_info_discount_and_save_money(f_xml, deal, price, value):
    deal_price = deal.getiterator(price)
    deal_value = deal.getiterator(value)
    f_xml.write('\t' * 2 + '<discount>%0.1f</discount>\n' % (float(deal_price[0].text) / float(deal_value[0].text) * 10))
    f_xml.write('\t' * 2 + '<save_money>%0.1f</save_money>\n' % (float(deal_value[0].text) - float(deal_price[0].text)))

def deal_info_img(f_xml, deal, image):
    deal_image = deal.getiterator(image)
    for node in deal_image:
	f_xml.write(('\t' * 2 + '<img><![CDATA[%s]]></img>\n' % (node.text)).encode('utf-8'))

def judge(f_id, d_id, deal, source):
    deal_id = deal.getiterator(source)
    if d_id.find(deal_id[0].text) > -1:
	return 1
    f_id.write((deal_id[0].text + '\n').encode('utf-8'))
    return 0

def judge_begin(tuan, city):
    if not os.path.isfile('%sxml/%s_%s.xml' % (get_home_dir(), tuan, city)):
	os.system('echo "<deals>\n</deals>" > %sxml/%s_%s.xml' % (get_home_dir(), tuan, city))
    os.system('cp %sxml/%s_%s.xml %sxml/%s_%s.xml.bak' % (get_home_dir(), tuan, city, get_home_dir(), tuan, city))
    f_orig = open('%sxml/%s_%s.xml.bak' % (get_home_dir(), tuan, city), 'r')
    orig = f_orig.read()
    f_orig.close()

    if not os.path.isfile('%slist/%s_%s_id' % (get_home_dir(), tuan, city)):
	os.system('touch %slist/%s_%s_id' % (get_home_dir(), tuan, city))
    f_id = open('%slist/%s_%s_id' % (get_home_dir(), tuan, city), 'r+a')
    d_id = f_id.read()

    return (f_id, d_id, orig)

def judge_end(tuan, city):
    f_xml = open('%sxml/%s_%s.xml' % (get_home_dir(), tuan, city), 'r')
    d_xml = f_xml.read()
    f_xml.close()
    if d_xml.find('<deals>\n</deals>') > -1:
	os.system('rm %sxml/%s_%s.xml' % (get_home_dir(), tuan, city))
    os.system('rm %sxml/%s_%s.xml.bak' % (get_home_dir(), tuan, city))

